package com.example.seey2023.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.seey2023.entity.Banji;
import com.example.seey2023.entity.User;
import com.example.seey2023.entity.XueYuan;
import com.example.seey2023.service.BanJiService;
import com.example.seey2023.service.RoleService;
import com.example.seey2023.service.UserService;
import com.example.seey2023.service.XueYuanService;
import com.example.seey2023.vo.DataView;
import com.example.seey2023.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private BanJiService banJiService;
    @Autowired
    private XueYuanService xueYuanService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/toUser")
    public String toUser() {
        return "user/user";
    }

    @RequestMapping("toChangePassword")
    public String toChangePassword() {
        return "user/changepassword";
    }

    @RequestMapping("toUserInfo")
    public String toUserInfo() {
        return "user/userInfo";
    }

    /**
     * 分页查询所有用户数据【姓名，电话】
     *
     * @param userVo
     * @return
     */
    @RequestMapping("/loadAllUser")
    @ResponseBody
    public DataView getAllUser(UserVo userVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        IPage<User> page = new Page<>(userVo.getPage(), userVo.getLimit());
        queryWrapper.like(StringUtils.isNotBlank(userVo.getUsername()), "username", userVo.getUsername());
        queryWrapper.eq(StringUtils.isNotBlank(userVo.getPhone()), "phone", userVo.getPhone());
        IPage<User> iPage = userService.page(page, queryWrapper);
        // 为banjiId赋值名称
        for (User user : iPage.getRecords()) {
            if (user.getBanJiId() != null) {
                // service查库
                Banji banji = banJiService.getById(user.getBanJiId());
                user.setBanJiName(banji.getName());
            }
            // 为xueyuanid赋值
            if (user.getXueYuanId() != null) {
                XueYuan xueYuan = xueYuanService.getById(user.getXueYuanId());
                user.setXueYuanName(xueYuan.getName());
            }
            // 为teacheid赋值
            if (user.getTeacherId() != null) {
                User teacher = userService.getById(user.getTeacherId());
                user.setXueYuanName(teacher.getUsername());
            }

        }
        return new DataView(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 新增用户
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public DataView addUser(User user) {
        userService.save(user);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("添加用户成功!");
        return dataView;
    }

    /**
     * 修改用户
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public DataView updateUser(User user) {
        userService.updateById(user);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("修改用户成功!");
        return dataView;
    }

    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser/{id}")
    @ResponseBody
    public DataView deleteUser(@PathVariable("id") Integer id) {
        userService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除用户成功!");
        return dataView;
    }

    /**
     * 初始化下拉列表的数据【班级】
     */
    @RequestMapping("/listAllBanJi")
    @ResponseBody
    public List<Banji> listAllBanJi() {
        List<Banji> list = banJiService.list();
        return list;
    }

    /**
     * 初始化下拉列表的数据【学院】
     */
    @RequestMapping("/listAllXueYuan")
    @ResponseBody
    public List<XueYuan> listAllXueYuan() {
        List<XueYuan> list = xueYuanService.list();
        return list;
    }

    /**
     * 重置密码
     */
    @RequestMapping("/resetPwd/{id}")
    @ResponseBody
    public DataView resetPwd(@PathVariable("id") Integer id) {
        User user = new User();
        user.setId(id);
        user.setPassword("123456");
        userService.updateById(user);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("重置密码成功!");
        return dataView;
    }

    /**
     * 修改密码
     */
    @RequestMapping("/changePassword")
    @ResponseBody
    public DataView changePassword(User user, String newPwdOne, String newPwdTwo) {
        // 1.查询数据库旧密码对不对
        User byId = userService.getById(user.getId());
        if (StringUtils.equals(byId.getPassword(),user.getPassword()) && StringUtils.equals(newPwdOne,newPwdTwo)){
            user.setPassword(newPwdOne);
            userService.updateById(user);
            DataView dataView = new DataView();
            dataView.setCode(200);
            dataView.setMsg("重置密码成功!");
            return dataView;
        }
        DataView dataView = new DataView();
        dataView.setCode(500);
        dataView.setMsg("重置密码失败，请检查密码后重新输入");
        return dataView;

//        Integer id = user.getId();
//        //user.setId(id);
//        user.setPassword("123456");
//        userService.updateById(user);
//        DataView dataView = new DataView();
//        dataView.setCode(200);
//        dataView.setMsg("重置密码成功!");
//        return dataView;
    }

    /**
     * 点击分配，先初始化用户角色
     * 打开分配角色的弹出层
     */
    @ResponseBody
    @RequestMapping("/initRoleByUserId")
    public DataView initRoleByUserId(Integer id) {
        // 查询所有角色
        List<Map<String, Object>> listMaps = roleService.listMaps();
        // 查询当前登录用户所拥有的角色
        List<Integer> currentUserRoleIds = roleService.queryUserRoleById(id);
        // 使前端变为选中状态
        for (Map<String, Object> map : listMaps) {
            Boolean LAY_CHECKED = false;
            Integer roleId = (Integer) map.get("id");
            for (Integer rid : currentUserRoleIds) {
                if (rid.equals(roleId)) {
                    LAY_CHECKED = true;
                    break;
                }
            }
            map.put("LAY_CHECKED", LAY_CHECKED);
        }
        return new DataView(Long.valueOf(listMaps.size()), listMaps);
    }

    /**
     * 保存用户：角色的关系 1：m
     */
    @RequestMapping("/saveUserRole")
    @ResponseBody
    public DataView saveUserRole(Integer uid, Integer[] ids) {
        userService.saveUserRole(uid, ids);
        DataView dataView = new DataView();
        dataView.setMsg("用户的角色分配成功");
        dataView.setCode(200);
        return dataView;
    }
}
